package com.eastjet.equipment.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastjet.equipment.service.DeviceLevelService;
import com.eastjet.maintenance.domain.DeviceFt;
import com.eastjet.maintenance.domain.DeviceLevel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/level")
public class LevelController {

    @Autowired
    private DeviceLevelService   deviceLevelService;

    @RequestMapping("/list")
    public Page<DeviceLevel> list
            (
                    @RequestBody Map<String, Object> map
            ) {
        System.out.println("map:" + map);

        // 检查 map 是否为空
        if (map == null || map.isEmpty()) {
            // 返回空的 Page<DeviceFt> 对象
            return  deviceLevelService.getDeviceLevelList(0, 0, "");
        }

        Integer page = (Integer) map.get("page");
        Integer size = (Integer) map.get("size");
        String condition = (String) map.get("condition");

        System.out.println("condition:" + condition);
        System.out.println("page:" + page);
        System.out.println("size:" + size);
        return deviceLevelService.getDeviceLevelList(page, size, condition);
    }


    @RequestMapping("/add")
    public String add(  @RequestBody Map<String, Object> map) {
        System.out.println("addmap:" + map);
        if (map == null || map.isEmpty()) {
            return "参数不能为空";
        }
        DeviceLevel deviceLevel = new DeviceLevel();
        System.out.println("addmap:" + map);
        deviceLevel.setLevelName((String) map.get("name"));
        deviceLevel.setCode((String) map.get("code"));
        deviceLevel.setLevelSort(  Integer.valueOf( map.get("sort").toString()));
        deviceLevel.setLevelState( Integer.valueOf( map.get("state").toString()));
        deviceLevel.setLevelNotes((String) map.get("notes"));
        return deviceLevelService.addDeviceLevel(deviceLevel);
    }

    @RequestMapping("/update")
    public String update(  @RequestBody Map<String, Object> map) {
        System.out.println("map:" + map);
        if (map == null || map.isEmpty()) {
            return "参数不能为空";
        }
        DeviceLevel deviceLevel = new DeviceLevel();
        deviceLevel.setCode((String) map.get("code"));
        // 使用 Integer.valueOf 转换整数
        deviceLevel.setLevelName((String) map.get("name"));
        deviceLevel.setLevelSort(Integer.valueOf(map.get("sort").toString()));
        deviceLevel.setLevelNotes((String) map.get("notes"));
        deviceLevel.setLevelState(Integer.valueOf(map.get("state").toString()));
        deviceLevel.setLevelId(Integer.valueOf(map.get("id").toString()));
        return deviceLevelService.updateDeviceLevel(deviceLevel);

    }

    @RequestMapping("/delete")
    public String delete(  @RequestBody Map<String, Object> map) {
        System.out.println("map:" + map);

        // 检查 map 是否为空
        if (map == null || map.isEmpty()) {
            return "参数不能为空";
        }

        // 获取 ids 列表
        List<Integer> ids = (List<Integer>) map.get("ids");

        // 创建 DeviceFt 对象并设置其状态
        DeviceLevel deviceLevel = new DeviceLevel();
        deviceLevel.setIsDelete(1);



        // 循环打印 ids 列表中的每个 ID
        if (ids != null) {
            for (Integer id : ids) {
                System.out.println("id: " + id);
                deviceLevel.setLevelId(id);
                deviceLevelService.deleteDeviceLevel(deviceLevel);
            }
            return "success";
        } else {
            System.out.println("ids 列表为空或未提供");
            return "参数错误";
        }
    }

    @RequestMapping("/updateState")
    public String updateState(  @RequestBody Map<String, Object> map) {
        System.out.println("map:" + map);
        // 检查 map 是否为空
        if (map == null || map.isEmpty()) {
            return "参数不能为空";
        }

        // 获取 state 值
        Integer state = (Integer) map.get("state");
        // 获取 ids 列表
        List<Integer> ids = (List<Integer>) map.get("ids");

        // 创建 DeviceFt 对象并设置其状态
        DeviceLevel deviceLevel = new DeviceLevel();
        deviceLevel.setLevelState(state);

        // 打印获取到的 state 值
        System.out.println("state: " + state);

        // 循环打印 ids 列表中的每个 ID
        if (ids != null) {
            for (Integer id : ids) {
                System.out.println("id: " + id);
                deviceLevel.setLevelId(id);
                deviceLevelService.updateDeviceLevelState(deviceLevel);
            }
            return "success";
        } else {
            System.out.println("ids 列表为空或未提供");
            return "参数错误";
        }
    }

}
